package com.know.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import my.db.QueryHelper;

import com.know.entity.Notice;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;

public class NoticeDao {

	/**
	 * 查询公告
	 * @return
	 */
	public List<Notice> list() {
		String sql="select *from notice where status=1";
		return QueryHelper.query(Notice.class, sql);
	}
	/**
	 * 分页查询
	 */
	public Record findPage(Page<Notice> page) {
		Record record = new Record();
		String sql = "select * from notice";
		if(StringUtils.isNotBlank(page.getOrderBy())) {
			sql += String.format(" order by %s %s", page.getOrderBy(), page.getOrder());
		}
		
		List<Notice> list = QueryHelper.queryPage(Notice.class, sql, page.getPageNo(), page.getPageSize());

	
		//统计总数count
		String sql2 = sql.toLowerCase().replaceFirst("select .*? from", "select count(*) from");
		long count = QueryHelper.stat(sql2);
		
		record.setCount(count);
		record.setData(list);
		
		return record;
	
	}
	/**
	 * 保存公告
	 */
	public void save(Notice notice) {
		String sql= "insert into notice values(null,?,?)";
		QueryHelper.update(sql,notice.getNotice_content(),notice.getStatus());
		
	
	}
	/**
	 * 修改标签
	 */
	public void update(Notice notice) {
		String sql="UPDATE notice SET notice_cotent=?, status=? WHERE (notice_id=?)";
		QueryHelper.update(sql, notice.getNotice_content(),notice.getStatus(),notice.getNotice_id());
	}
	/**
	 * 根据ID查找
	 */
	public Notice finById(long parseLong) {
		String sql="select *from notice where notice_id=?";
		Notice lable=QueryHelper.queryUnique(Notice.class, sql, parseLong);
		return lable;
	}
	/**
	 * 删除标签
	 */
	public Record  del(long id) {
		String sql="delete from notice where notice_id =?";
		String msg="成功删除";
		Record record=new Record(true);
	
		QueryHelper.update(sql, id);
		
		record.setMsg(msg);
		return record;
	}
	/**
	 * 批量删除
	 * @param ids
	 */
	public Record multiDel(String ids) {
		String sql = "delete from notice where notice_id in (" + ids + ")";
		String msg="成功删除";
		Record record=new Record(true);
		QueryHelper.update(sql);
		record.setMsg(msg);
		return record;
		
	}
	/**
	 * 改变状态
	 */
	public void changes(Notice notice) {
		String sql="UPDATE notice SET status=? WHERE (notice_id=?)";
		QueryHelper.update(sql,notice.getStatus(),notice.getNotice_id());
	}
	
}
